﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using MessageWorkflow;

namespace SimpleWpfExample.ViewModel
{
    class AuthenticateViewModel : ViewModelBase
    {
        public RelayCommand AuthenticateCommand { get; set; }

        public string UserName { get; set; }

        public string Password { get; set; }

        public AuthenticateViewModel()
        {
            AuthenticateCommand = new RelayCommand(AuthenticateUser, CanAuthenticateUser);
        }

        protected void AuthenticateUser()
        {
            UserViewModel authenticatedUser = ViewModelLocator.MainStatic.Users.SingleOrDefault(x => x.Name == UserName && x.Password == Password);

            if (authenticatedUser != null)
            {
                WorkflowChannel.Default.Dispatch(new Message.NewAuthenticatedUser()
                {
                    Data = authenticatedUser
                });
            }
        }

        protected bool CanAuthenticateUser()
        {
            return !string.IsNullOrEmpty(UserName) && !string.IsNullOrEmpty(Password);
        }
    }
}
